home *** CD-ROM | disk | FTP | other *** search
- unit Fexecbtn;
-
- interface
-
- uses
- SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
- Forms, Dialogs, StdCtrls, Buttons, ShellAPI;
-
- type
- TFileExecButton = class(TBitBtn)
- private
- { Private declarations }
- FTheFileName : String;
- FTheIcon : TIcon;
- FTheBitmap : TBitmap;
- FOnExecute : TNotifyEvent;
- protected
- { Protected declarations }
- public
- { Public declarations }
- constructor Create( AOwner : TComponent ); override;
- destructor Destroy; override;
- procedure Loaded; override;
- procedure ExecuteFile;
- published
- { Published declarations }
- Property TheFileName : String read FTheFileName write FTheFileName;
- Property OnExecute : TNotifyEvent read FOnExecute write FOnExecute;
- end;
-
- procedure Register;
-
- implementation
-
- function ShellExec(const PathStr, CmdStr, DirStr: string;
- PrintIt: boolean; Show: word; Wait: boolean): boolean;
- var
- Inst: THandle;
- Path, CmdLine, Dir: PChar;
- Op: array[0..5] of Char;
- begin
- if PrintIt then StrPCopy(Op, 'print') else StrPCopy(Op, 'open');
- { Get memory for PChars }
- GetMem(Path, Length(PathStr)+1);
- GetMem(CmdLine, Length(CmdStr)+1);
- GetMem(Dir, Length(DirStr)+1);
- try
- { Copy strings to PChars }
- StrPCopy(Path, PathStr);
- StrPCopy(CmdLine, CmdStr);
- StrPCopy(Dir, DirStr);
- { Execute file }
- Inst := ShellExecute(0, Op, Path, CmdLine, Dir, Show);
- { If 32 or less, an error occurred }
- if Inst <= 32 then Result := False else
- begin
- Result := True;
- end;
- finally
- { Ensure memory is freed }
- FreeMem(Path, Length(PathStr)+1);
- FreeMem(CmdLine, Length(CmdStr)+1);
- FreeMem(Dir, Length(DirStr)+1);
- end;
- end;
-
- constructor TFileExecButton.Create( AOwner : TComponent );
- begin
- inherited Create( AOwner );
- FTheIcon := TIcon.Create;
- FTheBitmap := TBitmap.Create;
- end;
-
- destructor TFileExecButton.Destroy;
- begin
- FTheIcon.Free;
- FTheBitmap.Free;
- inherited Destroy;
- end;
-
- procedure TFileExecButton.Loaded;
- var ThePChar : PChar;
- begin
- inherited Loaded;
- if not FileExists( TheFileName ) then exit;
- GetMem( ThePChar , 256 );
- FTheBitmap.Height := 32;
- FTheBitmap.Width := 32;
- StrPCopy( ThePChar , TheFileName );
- FTheIcon.Handle := ExtractIcon( hInstance , ThePChar , 0 );
- if FTheIcon.Handle <> 0 then
- begin
- FTheBitmap.Canvas.Draw( 1,1,FTheIcon );
- end;
- Glyph := FTheBitmap;
- Caption := ExtractFileName( TheFileName );
- Freemem( ThePChar , 256 );
- if Height < 40 then Height := 40;
- if Width < ( 60 + FTheBitmap.Canvas.TextWidth( TheFileName ) ) then
- Width := 60 + FTheBitmap.Canvas.TextWidth( TheFileName );
- end;
-
- procedure TFileExecButton.ExecuteFile;
- var
- TheActiveWindow : HWnd;
- TheWindowList : Pointer;
- begin
- TheWindowList := DisableTaskWindows( 0 );
- TheActiveWindow := GetActiveWindow;
- try
- if not ShellExec( TheFileName , '' , '', false ,
- SW_SHOWNORMAL , false ) then
- MessageDlg('Could not Execute Task!', mtInformation, [mbOK], 0);
- finally
- EnableTaskWindows( TheWindowList );
- SetActiveWindow( TheActiveWindow );
- end;
- end;
-
- procedure Register;
- begin
- RegisterComponents('Widgets', [TFileExecButton]);
- end;
-
- end.
-